﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_Emp3_Emp3BaseInfo : AdminPageBase
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetEmpId();
        if (!IsPostBack)
        {
            BindCenter();
            BindJob();
            BindBrand();
            BindCity();
            BindDept();
            BindStore();
            BindEmp();
            if (add == 1)
            {
                btnsave.Enabled = true;
            }
            else
            {
                btnsave.Enabled = false;
            }

        }
    }
    protected void BindCenter()
    {
        DataSet ds = jtDataAccess.SearchCenter("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlcenter.DataSource = ds;
            ddlcenter.DataTextField = "Name";
            ddlcenter.DataValueField = "CenterId";
            ddlcenter.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlcenter.Items.Insert(0, item);
    }
    protected void BindJob()
    {
        DataSet ds = jtDataAccess.SearchJob("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddljob.DataSource = ds;
            ddljob.DataTextField = "Name";
            ddljob.DataValueField = "JobId";
            ddljob.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddljob.Items.Insert(0, item);
    }
    protected void BindBrand()
    {
        DataSet ds = jtDataAccess.SearchBrand("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlbrand.DataSource = ds;
            ddlbrand.DataTextField = "Name";
            ddlbrand.DataValueField = "BrandId";
            ddlbrand.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlbrand.Items.Insert(0, item);
    }
    protected void BindCity()
    {
        DataSet ds = jtDataAccess.SearchCity("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlcity.DataSource = ds;
            ddlcity.DataTextField = "Name";
            ddlcity.DataValueField = "CityId";
            ddlcity.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlcity.Items.Insert(0, item);
    }
    protected void BindDept()
    {
        DataSet ds = jtDataAccess.SearchDept("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddldept.DataSource = ds;
            ddldept.DataTextField = "Name";
            ddldept.DataValueField = "DeptId";
            ddldept.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddldept.Items.Insert(0, item);
    }
    protected void BindStore()
    {
        DataSet ds = jtDataAccess.SearchStore("", 10, 0, "Code", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
           ddlstore.DataSource = ds;
           ddlstore.DataTextField = "Code";
           ddlstore.DataValueField = "StoreId";
           ddlstore.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlstore.Items.Insert(0, item);
    }
    protected void GetEmpId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;

        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);

        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindEmp()
    {
        if (id != 0)
        {
            DataSet ds = jtDataAccess.GetEmpInfo(id);
            if (CoreTools.CheckDataSet(ds))
            {
                txtcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                txtentrydate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["EntryDate"].ToString()).ToString("yyyy-MM-dd").Replace("1900-01-01", "");
                txtleavedate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["LeaveDate"].ToString()).ToString("yyyy-MM-dd").Replace("2099-12-31", "").Replace("1900-01-01", "");
                txtName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                ddlbrand.SelectedValue = ds.Tables[0].Rows[0]["BrandId"].ToString();
                ddlcenter.SelectedValue = ds.Tables[0].Rows[0]["CenterId"].ToString();
                ddlcity.SelectedValue = ds.Tables[0].Rows[0]["CityId"].ToString();
                ddlstore.SelectedValue = ds.Tables[0].Rows[0]["StoreId"].ToString();
                ddldept.SelectedValue = ds.Tables[0].Rows[0]["DeptId"].ToString();
                ddljob.SelectedValue = ds.Tables[0].Rows[0]["JobId"].ToString();
                rbtlFinishClass.SelectedValue = ds.Tables[0].Rows[0]["FinishClass"].ToString();
                rbtlIsTeacher.SelectedValue = ds.Tables[0].Rows[0]["IsTeacher"].ToString();

            }
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string name = txtName.Text.Trim();
        string code = txtcode.Text.Trim();
        int centerid = Convert.ToInt32(ddlcenter.SelectedValue);
        int isteacher = Convert.ToInt32(rbtlIsTeacher.SelectedValue);
        int finishclass = Convert.ToInt32(rbtlFinishClass.SelectedValue);
        int brandid = Convert.ToInt32(ddlbrand.SelectedValue);
        int jobid = Convert.ToInt32(ddljob.SelectedValue);
        int deptid = Convert.ToInt32(ddldept.SelectedValue);
        int cityid = Convert.ToInt32(ddlcity.SelectedValue);
        int storeid = Convert.ToInt32(ddlstore.SelectedValue);
        DateTime entrydate = DateTime.MinValue;
        if (txtentrydate.Text.Trim() != "")
            entrydate = DateTime.Parse(txtentrydate.Text.Trim());
        DateTime leavedate = DateTime.MinValue;
        if (txtleavedate.Text.Trim() != "")
            leavedate = Convert.ToDateTime(txtleavedate.Text.Trim());
        if (!CoreTools.ProcessSqlStr(name, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtName.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(code, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtcode.Focus();
        }
        else
        {
            JTDataAccess.OpType OpType;
            if (id == 0)
            {
                OpType = JTDataAccess.OpType.Insert;

            }
            else
            {
                OpType = JTDataAccess.OpType.Update;

            }

            int flag = jtDataAccess.OpEmp(OpType, id, name, code, 0, 3, 0, cityid, deptid, storeid, brandid, jobid, entrydate, leavedate, isteacher, finishclass);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，该员工已经存在！"), false);

                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                    break;
                default:
                    // ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", "<script>alert('操作成功！'); parent.document.getElementById('hidempid').value="+ flag + ";</script>", false);
                    break;
            }
        }
    }
   
  
    protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cityid = ddlcity.SelectedValue;

        DataSet ds = jtDataAccess.GetList("City", "CityId=" + int.Parse(cityid), "Name");
        if (CoreTools.CheckDataSet(ds))
            ddlcenter.SelectedValue = ds.Tables[0].Rows[0]["CenterId"].ToString();
    }
}